@echo off
REM 测试运行脚本 (Windows版本)

setlocal enabledelayedexpansion

REM 颜色定义
set "RED=[ERROR]"
set "GREEN=[SUCCESS]"
set "YELLOW=[WARNING]"
set "BLUE=[INFO]"

REM 默认参数
set "VENV_NAME=venv"
set "TEST_TYPE=all"
set "COVERAGE=false"
set "VERBOSE=false"
set "TEST_PATH=tests/"

REM 显示帮助信息
:show_help
echo 测试运行脚本 (Windows版本)
echo.
echo 用法: %0 [选项]
echo.
echo 选项:
echo     /h, /help              显示帮助信息
echo     /venv ^<name^>           虚拟环境名称 (默认: venv)
echo     /type ^<type^>           测试类型: unit, integration, e2e, all (默认: all)
echo     /path ^<path^>           测试路径 (默认: tests/)
echo     /coverage              生成覆盖率报告
echo     /verbose               详细输出
echo.
echo 示例:
echo     %0                     运行所有测试
echo     %0 /type unit          只运行单元测试
echo     %0 /coverage           运行测试并生成覆盖率报告
echo     %0 /path tests/unit    运行指定路径的测试
goto :eof

REM 解析命令行参数
:parse_args
if "%~1"=="" goto :check_params
if /i "%~1"=="/h" goto :show_help
if /i "%~1"=="/help" goto :show_help
if /i "%~1"=="/venv" (
    set "VENV_NAME=%~2"
    shift
    shift
    goto :parse_args
)
if /i "%~1"=="/type" (
    set "TEST_TYPE=%~2"
    shift
    shift
    goto :parse_args
)
if /i "%~1"=="/path" (
    set "TEST_PATH=%~2"
    shift
    shift
    goto :parse_args
)
if /i "%~1"=="/coverage" (
    set "COVERAGE=true"
    shift
    goto :parse_args
)
if /i "%~1"=="/verbose" (
    set "VERBOSE=true"
    shift
    goto :parse_args
)
echo %RED% 未知参数: %~1
goto :show_help

:check_params
REM 参数验证
if /i not "%TEST_TYPE%"=="unit" if /i not "%TEST_TYPE%"=="integration" if /i not "%TEST_TYPE%"=="e2e" if /i not "%TEST_TYPE%"=="all" (
    echo %RED% 无效的测试类型: %TEST_TYPE%
    echo 支持的类型: unit, integration, e2e, all
    exit /b 1
)

REM 日志函数
:log_info
echo %BLUE% %~1
goto :eof

:log_success
echo %GREEN% %~1
goto :eof

:log_warning
echo %YELLOW% %~1
goto :eof

:log_error
echo %RED% %~1
goto :eof

REM 检查虚拟环境
:check_venv
call :log_info "检查虚拟环境..."

if not exist "%VENV_NAME%" (
    call :log_error "虚拟环境 %VENV_NAME% 不存在"
    echo 请先运行 setup_venv.bat 创建虚拟环境
    exit /b 1
)

if not exist "%VENV_NAME%\Scripts\activate.bat" (
    call :log_error "虚拟环境激活脚本不存在"
    exit /b 1
)

call :log_success "虚拟环境检查通过"
goto :eof

REM 激活虚拟环境
:activate_venv
call :log_info "激活虚拟环境..."
call "%VENV_NAME%\Scripts\activate.bat"
goto :eof

REM 检查测试环境
:check_test_environment
call :log_info "检查测试环境..."

REM 检查pytest是否安装
pytest --version >nul 2>&1
if errorlevel 1 (
    call :log_error "pytest未安装"
    echo 请在虚拟环境中安装: pip install pytest
    exit /b 1
)

REM 检查测试目录
if not exist "%TEST_PATH%" (
    call :log_error "测试目录不存在: %TEST_PATH%"
    exit /b 1
)

call :log_success "测试环境检查通过"
goto :eof

REM 设置测试环境变量
:set_test_env
call :log_info "设置测试环境变量..."

set "TESTING=1"
set "DATABASE_URL=sqlite:///./test_campus_video_twin.db"
set "DISABLE_GPU_TASKS=1"
set "DISABLE_EXTERNAL_SERVICES=1"

call :log_success "测试环境变量设置完成"
goto :eof

REM 运行单元测试
:run_unit_tests
call :log_info "运行单元测试..."

set "test_path=tests/unit"
if not exist "%test_path%" (
    call :log_warning "单元测试目录不存在: %test_path%"
    goto :eof
)

set "pytest_args=-v"
if /i "%VERBOSE%"=="true" set "pytest_args=%pytest_args% -s"
if /i "%COVERAGE%"=="true" set "pytest_args=%pytest_args% --cov=. --cov-report=html --cov-report=term"

echo 执行命令: pytest %test_path% %pytest_args%
pytest %test_path% %pytest_args%
if errorlevel 1 (
    call :log_error "单元测试失败"
    set "test_failed=true"
) else (
    call :log_success "单元测试通过"
)
goto :eof

REM 运行集成测试
:run_integration_tests
call :log_info "运行集成测试..."

set "test_path=tests/integration"
if not exist "%test_path%" (
    call :log_warning "集成测试目录不存在: %test_path%"
    goto :eof
)

set "pytest_args=-v --tb=short"
if /i "%VERBOSE%"=="true" set "pytest_args=%pytest_args% -s"

echo 执行命令: pytest %test_path% %pytest_args%
pytest %test_path% %pytest_args%
if errorlevel 1 (
    call :log_error "集成测试失败"
    set "test_failed=true"
) else (
    call :log_success "集成测试通过"
)
goto :eof

REM 运行端到端测试
:run_e2e_tests
call :log_info "运行端到端测试..."

set "test_path=tests/e2e"
if not exist "%test_path%" (
    call :log_warning "端到端测试目录不存在: %test_path%"
    goto :eof
)

set "pytest_args=-v --tb=short -m e2e"
if /i "%VERBOSE%"=="true" set "pytest_args=%pytest_args% -s"

echo 执行命令: pytest %test_path% %pytest_args%
pytest %test_path% %pytest_args%
if errorlevel 1 (
    call :log_error "端到端测试失败"
    set "test_failed=true"
) else (
    call :log_success "端到端测试通过"
)
goto :eof

REM 运行性能测试
:run_performance_tests
call :log_info "运行性能测试..."

set "test_path=tests/performance"
if not exist "%test_path%" (
    call :log_warning "性能测试目录不存在: %test_path%"
    goto :eof
)

set "pytest_args=-v --tb=short -m performance"
if /i "%VERBOSE%"=="true" set "pytest_args=%pytest_args% -s"

echo 执行命令: pytest %test_path% %pytest_args%
pytest %test_path% %pytest_args%
if errorlevel 1 (
    call :log_error "性能测试失败"
    set "test_failed=true"
) else (
    call :log_success "性能测试通过"
)
goto :eof

REM 运行所有测试
:run_all_tests
call :log_info "运行所有测试..."

set "pytest_args=-v"
if /i "%VERBOSE%"=="true" set "pytest_args=%pytest_args% -s"
if /i "%COVERAGE%"=="true" set "pytest_args=%pytest_args% --cov=. --cov-report=html --cov-report=term"

echo 执行命令: pytest %TEST_PATH% %pytest_args%
pytest %TEST_PATH% %pytest_args%
if errorlevel 1 (
    call :log_error "测试失败"
    set "test_failed=true"
) else (
    call :log_success "所有测试通过"
)
goto :eof

REM 生成测试报告
:generate_test_report
if /i "%COVERAGE%"=="true" (
    call :log_info "生成测试报告..."
    
    if exist "htmlcov" (
        call :log_success "覆盖率报告已生成: htmlcov/index.html"
    )
    
    if exist "coverage.xml" (
        call :log_success "覆盖率XML报告: coverage.xml"
    )
)
goto :eof

REM 清理测试环境
:cleanup_test_env
call :log_info "清理测试环境..."

REM 删除测试数据库
if exist "test_campus_video_twin.db" (
    del "test_campus_video_twin.db"
    call :log_info "测试数据库已清理"
)

REM 清理临时文件
if exist ".pytest_cache" (
    rmdir /s /q ".pytest_cache"
    call :log_info "pytest缓存已清理"
)

call :log_success "测试环境清理完成"
goto :eof

REM 显示测试结果摘要
:show_test_summary
echo.
echo ================================================
echo 测试结果摘要
echo ================================================
echo 测试类型: %TEST_TYPE%
echo 测试路径: %TEST_PATH%
echo 执行时间: %date% %time%

if defined test_failed (
    echo 测试状态: 失败
    echo ================================================
    call :log_error "部分测试失败，请检查上述输出"
) else (
    echo 测试状态: 通过
    echo ================================================
    call :log_success "所有测试都通过了！"
)

if /i "%COVERAGE%"=="true" (
    echo.
    echo 覆盖率报告: htmlcov/index.html
)
goto :eof

REM 主函数
:main
title 校园视频孪生系统 - 测试运行

call :log_info "开始运行校园视频孪生系统测试..."

REM 检查虚拟环境
call :check_venv
if errorlevel 1 exit /b 1

REM 激活虚拟环境
call :activate_venv

REM 检查测试环境
call :check_test_environment
if errorlevel 1 exit /b 1

REM 设置测试环境变量
call :set_test_env

REM 根据测试类型运行相应测试
if /i "%TEST_TYPE%"=="unit" (
    call :run_unit_tests
) else if /i "%TEST_TYPE%"=="integration" (
    call :run_integration_tests
) else if /i "%TEST_TYPE%"=="e2e" (
    call :run_e2e_tests
) else if /i "%TEST_TYPE%"=="performance" (
    call :run_performance_tests
) else if /i "%TEST_TYPE%"=="all" (
    call :run_all_tests
) else (
    REM 运行指定路径的测试
    call :run_all_tests
)

REM 生成测试报告
call :generate_test_report

REM 清理测试环境
call :cleanup_test_env

REM 显示测试结果摘要
call :show_test_summary

REM 根据测试结果设置退出码
if defined test_failed (
    exit /b 1
) else (
    exit /b 0
)

goto :eof

REM 解析参数并执行主函数
call :parse_args %*
call :main

pause